home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / lecuyer21.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-02  |  2.2 KB  |  99 lines

  1. /* rng/lecuyer21.c
  2.  * 
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or (at
  6.  * your option) any later version.
  7.  * 
  8.  * This program is distributed in the hope that it will be useful, but
  9.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.  * General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /*
  19.  * This generator is taken from
  20.  *
  21.  * Donald E. Knuth
  22.  * The Art of Computer Programming
  23.  * Volume 2
  24.  * Third Edition
  25.  * Addison-Wesley
  26.  * Page 108
  27.  *
  28.  * This implementation copyright (C) 2001 Brian Gough, Carlo Perassi.
  29.  */
  30.  
  31. #include <config.h>
  32. #include <stdlib.h>
  33. #include <gsl/gsl_rng.h>
  34.  
  35. #define AAA 40692
  36. #define MMM 2147483399UL
  37. #define QQQ 52774
  38. #define RRR 3791
  39.  
  40. static inline unsigned long int ran_get (void *vstate);
  41. static double ran_get_double (void *vstate);
  42. static void ran_set (void *state, unsigned long int s);
  43.  
  44. typedef struct
  45. {
  46.   unsigned long int x;
  47. }
  48. ran_state_t;
  49.  
  50. static inline unsigned long int
  51. ran_get (void *vstate)
  52. {
  53.   ran_state_t *state = (ran_state_t *) vstate;
  54.  
  55.   long int y = state->x;
  56.   long int r = RRR * (y / QQQ);
  57.  
  58.   y = AAA * (y % QQQ) - r;
  59.   if (y < 0)
  60.     y += MMM;
  61.  
  62.   state->x = y;
  63.  
  64.   return state->x;
  65. }
  66.  
  67. static double
  68. ran_get_double (void *vstate)
  69. {
  70.   ran_state_t *state = (ran_state_t *) vstate;
  71.  
  72.   return ran_get (state) / 2147483400.0;
  73. }
  74.  
  75. static void
  76. ran_set (void *vstate, unsigned long int s)
  77. {
  78.   ran_state_t *state = (ran_state_t *) vstate;
  79.  
  80.   if (s == 0)
  81.     s = 1;            /* default seed is 1 */
  82.  
  83.   state->x = s & MMM;
  84.  
  85.   return;
  86. }
  87.  
  88. static const gsl_rng_type ran_type = {
  89.   "lecuyer21",            /* name */
  90.   MMM,                /* RAND_MAX */
  91.   0,                /* RAND_MIN */
  92.   sizeof (ran_state_t),
  93.   &ran_set,
  94.   &ran_get,
  95.   &ran_get_double
  96. };
  97.  
  98. const gsl_rng_type *gsl_rng_lecuyer21 = &ran_type;
  99.